import com.sun.security.auth.NTNumericCredential;

import java.net.PortUnreachableException;
import java.util.*;

public class BianarTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode creat() {

        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = H;
        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    List<Character> list=new ArrayList<>();
    public List<Character> preOrder1(TreeNode root){
        if (root==null){
            return list;
        }
        list.add(root.val);
        preOrder1(root.left);
        preOrder1(root.right);
        return list;
    }
    public List<Character> preOrder2(TreeNode root){
        List<Character> list1=new ArrayList<>();
        if (root==null){
            return list1;
        }
        List<Character> leftTree=preOrder2(root.left);
        list.addAll(leftTree);
        List<Character> rightTree=preOrder2(root.right);
        list.addAll(rightTree);
        return list;
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.val + " ");
        inOrder((root.right));
    }

    //后序遍历
    public void nextOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        nextOrder(root.left);
        nextOrder(root.right);
        System.out.println(root.val + " ");
    }

    public int useside = 0;

    // 获取树中节点的个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        useside++;
        size(root.left);
        size(root.right);
        return useside;
    }

    public int size1(TreeNode root){
        if (root==null){
            return 0;
        }
        return size1(root.left)+size1(root.right)+1;
    }
    // 获取叶⼦节点的个数
    public int lefecount = 0;

    public int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    public void getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            useside++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);

    }

// 获取第K层节点的个数

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    // 获取⼆叉树的⾼度
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftheight = getHeight(root.left);
        int rightheight = getHeight(root.right);
        return Math.max(leftheight, leftheight) +1;

    }

    public int getHeight1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftheight = getHeight1(root.left);
        int rightheight = getHeight1(root.right);
        return leftheight > rightheight ? leftheight+1 : rightheight+1;
        // return getHeight1(root.left)>getHeight1(root.right)?getHeight1(root.left)+1:getHeight(root.right)+1h;
    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode ret = find(root.left, val);
        if (ret != null) {
            return ret;
        }
        TreeNode ret1 = find(root.right, val);
        if (ret1 != null) {
            return ret1;
        }
        return null;
    }





    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p != null && q == null || q != null && p == null) {
            return false;
        }
        if (p == null && q == null) {
            return true;
        }
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);

    }

    //是否有子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        if (isSameTree(root, subRoot)) {
            return true;
        }
        return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);

    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode right;
        TreeNode left;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

    public TreeNode invertTree1(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode Left = invertTree(root.left);
        TreeNode Right = invertTree1(root.right);
        root.right = Left;
        root.left = Right;
        return root;
    }
public boolean isbanlenced(TreeNode root){
        if (root==null){
            return true;
        }
        int leftHight=getHeight(root.right);
        int rightHight=getHeight(root.right);
        return Math.abs(leftHight-rightHight)<=1&&isbanlenced(root.left)&&isbanlenced(root.right);
}
    //判断是否是平衡二叉树
    public boolean isBalanced2(TreeNode root) {
return checkBalanced(root)!=-1;
    }

    public int checkBalanced(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int lefthight = checkBalanced(root.left);
        int righthight = checkBalanced(root.right);
        if (lefthight == -1 || righthight == -1) {
            return -1;
        }
        if (Math.abs(lefthight - righthight) > 1) {
            return -1;
        }
        return Math.max(lefthight, righthight)+1;
    }
    public boolean isSymmetric(TreeNode root) {
if (root==null){
    return true;
}
return isSymmetricTree(root.left,root.right);
    }
    public boolean isSymmetricTree(TreeNode left,TreeNode right){
        if (left==null||right==null){
            return true;
        }
        if (left!=null&&right==null||right!=null&&left==null){
            return false;
        }
        if (left.val!=right.val){
            return false;
        }
        return isSymmetricTree(left.left,right.right)&&isSymmetricTree(left.right,right.left);
    }

      public void levelOrder(TreeNode root){
        if (root==null){
            return;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.println(cur.val);
            if (cur.left!=null){
                queue.offer(cur.left);
            }
            if (cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }
    public List<List<Character>> levelOrder1(TreeNode root) {
         List<List<Character>> ret=new ArrayList<>();
         if (root==null){
             return ret;
         }
         Queue<TreeNode> queue=new LinkedList<>();
         queue.offer(root);
         while (!queue.isEmpty()){
             List<Character> list=new ArrayList<>();
             int size=queue.size();
             while (size!=0) {
                 TreeNode cur = queue.poll();
                 list.add(cur.val);
                 if (cur.right!=null){
                     queue.offer(cur.right);
                 }
                 if (cur.left!=null){
                     queue.offer(cur.left);
                 }
                 size--;
             }
             ret.add(list);
         }
         return ret;
    }
    //公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
                        if(root==null){
                            return root;
                        }
                        if (root==q||root==p){
                            return root;
                        }
                        TreeNode left=lowestCommonAncestor(root.left,p,q);
                        TreeNode right=lowestCommonAncestor(root.right,p,q);
                        if (left!=null&&right!=null){
                            return root;
                        }
                        else if (left!=null){
                            return left;
                        }
                        else if (right!= null){
                            return right;
                        }
                        return null;
    }
    public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p,TreeNode q){
        if (root==null){
            return null;
        }
        Stack<TreeNode> stackp=new Stack<>();
        Stack<TreeNode> stackq=new Stack<>();
        getpath(root,p,stackp);
        getpath(root,q,stackq);
        int sizep=stackp.size();
        int sizeq=stackq.size();

        if (sizep>sizeq){
            int size=sizep-sizeq;
            while (size!=0){
                stackp.pop();
            }
        }else {
            int size=sizeq-sizep;
            while (size!=0){
                stackq.pop();
            }
            while (!stackp.isEmpty()){
                if (stackp.peek()==stackq.peek()){
                    return stackp.peek();
                }else {
                    stackp.pop();
                    stackq.pop();
                }
            }

        } return null;
    }
    public boolean getpath(TreeNode root,TreeNode node,Stack stack){
        if (root==null||node==null){
            return false;
        }
        stack.push(root);
        if (root==node){
            return true;
        }
        boolean flag= getpath(root.left,node,stack);
        if (flag){
            return true;
        }
        boolean flag1=getpath(root.right,node,stack);
        if (flag1){
            return true;
        }
        stack.pop();
        return false;
    }



}

